const coon = require('../db/index.js')
const shouIndexPage = function(req, res) {
    // 每页默认显示3条
    const pagesize = 3
        const newpage =Number(req.query.page || 1)
        const sql =`SELECT blog_atricles.id,blog_atricles.title,blog_atricles.ctime,login1.nicktime
    FROM blog_atricles LEFT JOIN login1
    ON blog_atricles.authorld = login1.id
    ORDER BY blog_atricles.id DESC LIMIT ${(newpage-1)*pagesize}, ${pagesize};
    SELECT count(*) as count FROM blog_atricles`
    coon.conn.query(sql,(err,result)=>{
        if (err) {
            return res.render('index.ejs',{
                user:req.session.user,
                isLogin:req.session.isLogin,
                // 文章列表
                articles: []
            })
        }
        // 总条数
        const totalpage = Math.ceil(result[1][0].count/pagesize)
        console.log('totalpage', totalpage)
        console.log('result', result[1][0].count)
        return res.render('index.ejs',{
            user:req.session.user,
            isLogin:req.session.isLogin,
            // 文章列表
            articles: result[0],
            // 总页数
            totalPage: totalpage,
            // 默认展示第几页
            nowpage:newpage
        })
    })
}
module.exports = {
    shouIndexPage
}